package cn.eric.game.swing;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class TestButton {
	private JFrame mainFrame;
	   private JLabel headerLabel;
	   private JLabel statusLabel;
	   private JPanel controlPanel;

	   public TestButton(){
	      prepareGUI();
	   }

	   public static void main(String[] args){
		  TestButton  swingControlDemo = new TestButton();      
	      swingControlDemo.showButtonDemo();
	   }

	   private void prepareGUI(){
	      mainFrame = new JFrame("Java Swing Examples");
	      mainFrame.setSize(400,400);
	      mainFrame.setLayout(new GridLayout(3, 1));
	      mainFrame.addWindowListener(new WindowAdapter() {
	         public void windowClosing(WindowEvent windowEvent){
	            System.exit(0);
	         }        
	      });
	      headerLabel = new JLabel("", JLabel.CENTER);
	      //headerLabel.setOpaque(true); 
	      //headerLabel.setBackground(Color.GREEN);
	      ImageIcon background = createImageIcon("/resources/header.jpg","Java");
	      background.setImage(background.getImage().getScaledInstance(400,200,  
                  Image.SCALE_DEFAULT));
	      headerLabel.setIcon(background);  
	      headerLabel.setHorizontalAlignment(0);   
	      statusLabel = new JLabel("",JLabel.CENTER);    
	      statusLabel.setSize(350,100);
	      statusLabel.setOpaque(true);
	      statusLabel.setBackground(Color.ORANGE);
	      controlPanel = new JPanel();
	      controlPanel.setLayout(new FlowLayout());
	      controlPanel.setOpaque(true);
	      controlPanel.setBackground(Color.WHITE);
	      mainFrame.add(headerLabel);
	      mainFrame.add(statusLabel);
	      mainFrame.add(controlPanel);

	      mainFrame.setVisible(true);  
	   }
	    
	   private static ImageIcon createImageIcon(String path, 
	      String description) {
	      java.net.URL imgURL = TestButton.class.getResource(path);
	      if (imgURL != null) {
	         return new ImageIcon(imgURL, description);
	      } else {            
	         System.err.println("Couldn't find file: " + path);
	         return null;
	      }
	   }   

	   private void showButtonDemo(){

	      //headerLabel.setText("欢迎来到三国富甲天下6"); 
	      
	      //resources folder should be inside SWING folder.
	      ImageIcon iconStart = createImageIcon("/resources/start.png","Java");
	      iconStart.setImage(iconStart.getImage().getScaledInstance(20, 20,  
                  Image.SCALE_DEFAULT));
	      ImageIcon iconProcess = createImageIcon("/resources/process.ico","Java");
	      iconProcess.setImage(iconProcess.getImage().getScaledInstance(20, 20,  
	    		  Image.SCALE_DEFAULT));
	      ImageIcon iconDesc = createImageIcon("/resources/desc.ico","Java");
	      iconDesc.setImage(iconDesc.getImage().getScaledInstance(20, 20,  
	    		  Image.SCALE_DEFAULT));
	      
	      
	      
	      JButton startButton = new JButton("开始游戏", iconStart);        
	      JButton getButton = new JButton("读取进度", iconProcess);
	      JButton descButton = new JButton("游戏说明", iconDesc);
	      descButton.setHorizontalTextPosition(SwingConstants.LEFT);   

	      startButton.addActionListener(new ActionListener() {
	         public void actionPerformed(ActionEvent e) {
	            statusLabel.setText("Game start");
	         }          
	      });

	      getButton.addActionListener(new ActionListener() {
	         public void actionPerformed(ActionEvent e) {
	            statusLabel.setText("get saves");
	         }
	      });

	      descButton.addActionListener(new ActionListener() {
	         public void actionPerformed(ActionEvent e) {
	            statusLabel.setText("This is a game");
	         }
	      });

	      controlPanel.add(startButton);
	      controlPanel.add(getButton);
	      controlPanel.add(descButton);       

	      mainFrame.setVisible(true);  
	   }
}
